package 
{
	import flash.ui.Keyboard
	import flash.display.Sprite;
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.PressAndTapGestureEvent;
	import flash.events.TimerEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import com.junkbyte.console.Cc;
	import flash.utils.Timer;
	import ping.PingHelper;
	import com.quetwo.Arduino.ArduinoConnector;
	import com.quetwo.Arduino.ArduinoConnectorEvent;
	
	/**
	 * @author yaempechenki@gmail.com
	 */
	public class Redbutton extends Sprite 
	{
		private const NO_URL:String = "http://www.nooooooooooooooo.com/";
		private const YES_URL:String = "http://yeeeeeeeeeeeeeees.com/";
		
		private var portTimer:Timer;
		private var arduino:ArduinoConnector;
		
		private var view:View;
		private var trayIconHelper:TrayIconHelper;
		private var pingHelper:PingHelper;
		
		private var lastComand:String = "";
		
		public function Redbutton():void 
		{
			initApp();
		}
		
		private function initApp():void
		{
			Cc.start( this, '`' );
			Cc.x = Cc.y = 2;
			Cc.width = 300 - Cc.x * 2;
			Cc.height = 200 - Cc.y * 2;
			Cc.config.alwaysOnTop = true;
			Cc.config.maxLines = 10000;
			Cc.visible = true;
			Cc.log("Выбери com порт!");
			
			view = new View();
			view.addEventListener(AppEvent.APP_CLOSE, exitApplication);
			view.addEventListener(AppEvent.APP_TO_TRAY, goToTray);
			view.addEventListener(AppEvent.APP_COMMAND_ON, sendCommandToRs232);
			view.addEventListener(AppEvent.APP_COMMAND_OFF, sendCommandToRs232);
			view.addEventListener(AppEvent.APP_PORT_SELECT, setPort);
			addChild(view);
			
			pingHelper = new PingHelper(PingHelper.VK);
			pingHelper.addEventListener(AppEvent.APP_COMMAND_ON, pingerEventHandler);
			pingHelper.addEventListener(AppEvent.APP_COMMAND_OFF, pingerEventHandler);
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, drugnme);
			trayIconHelper = new TrayIconHelper(stage);
			trayIconHelper.addEventListener(AppEvent.APP_CLOSE, exitApplication);

			arduino = new ArduinoConnector();
			var ports:Array = [];
			ports = arduino.getComPorts();
			ports.unshift("Select port..");
			view.showPorts(ports);
			
			portTimer = new Timer(1000);
			portTimer.addEventListener(TimerEvent.TIMER, onTimerTick);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		private function connect():void {
			arduino.connect(view.port, Settings.PORT_SPEED);			
			portTimer.start();
		}
		
		private function sendCommandToRs232(event:Event):void {
			Cc.log("send command: " + event.type);
			if(!arduino.portOpen) return;
			if (event.type == AppEvent.APP_COMMAND_ON) {
				arduino.writeString("a");
			}
			if (event.type == AppEvent.APP_COMMAND_OFF) {
				arduino.writeString("b");
			}
		}
		
		private function pingerEventHandler(event:Event):void {
			if (event.type != lastComand) {
				lastComand = event.type;
				sendCommandToRs232(event);
			}
		}
		
		private function onTimerTick(timer:TimerEvent):void {
			if (!arduino) return;
			var str:String = arduino.readBytesAsString();
			if (str.length > 0) {
				Cc.log("receive ", str);
				if (str.search("down") != -1) {
					view.somenameOfMethod();
					var gotoUrl:String = Math.random() > 0.5? YES_URL: NO_URL;
					navigateToURL(new URLRequest(gotoUrl), "_blank");
				}
			}
		}
		
		private function setPort(event:Event):void {
			connect();
		}
		
		private function goToTray(event:Event):void {
			trayIconHelper.showTray();
		}
		
		private function exitApplication(event:Event):void {
			arduino.dispose();
			trayIconHelper.fromTray();
			NativeApplication.nativeApplication.exit();
		}
		
		private function drugnme(event:MouseEvent):void {
			if (mouseY > 20) return;
			stage.nativeWindow.startMove()
		}
		
		private function onKeyDown(event:KeyboardEvent):void 
		{
			if (event.keyCode == Keyboard.UP) {
				sendCommandToRs232(new Event(AppEvent.APP_COMMAND_ON));
			}
			if (event.keyCode == Keyboard.DOWN) {
				sendCommandToRs232(new Event(AppEvent.APP_COMMAND_OFF));
			}
		}
	}
	
}